package com.certification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.certification.dto.CorrectImproveDTO;
import com.certification.entity.Certification;
import com.certification.entity.CorrectImprove;
import com.certification.entity.ProfessionSelfEvaluation;
import com.certification.mapper.CorrectImproveMapper;
import com.certification.service.CertificationService;
import com.certification.service.CorrectImproveService;
import com.certification.service.ProfessionSelfEvaluationService;
import com.certification.vo.CorrectImproveAdminVO;
import com.certification.vo.CorrectImproveVO;
import com.certification.vo.ResponseMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

import static com.certification.constants.CertConstant.CORRECT_IMPROVE;
import static com.certification.constants.CertConstant.SCHOOL_INVESTIGATE;
import static com.certification.constants.CertStatusConstant.CERTING;
import static com.certification.constants.CertStatusConstant.CERT_SUCCESS;


/**
 * @author copa
 * @createDate 2022-03-30 22:24
 * @function
 */
@Service
public class CorrectImproveServiceImpl implements CorrectImproveService {

    @Resource
    private CorrectImproveMapper correctImproveMapper;

    @Resource
    private CertificationService certificationService;

    @Resource
    private ProfessionSelfEvaluationService professionSelfEvaluationService;

    @Override
    public ResponseMessage<CorrectImproveVO> getImprove(Integer userId) {
        CorrectImproveVO correctImproveVO = correctImproveMapper.selectImprove(userId);
        return ResponseMessage.success(correctImproveVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseMessage<String> addImprove(CorrectImproveDTO correctImproveDTO, Integer userId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                SCHOOL_INVESTIGATE, userId, null, CERT_SUCCESS);
        if (Objects.isNull(cert)) {
            return ResponseMessage.errorMsg("请求错误~没有该数据~");
        }
        if (Objects.isNull(correctImproveDTO.getCorrectExplain())
                || Objects.isNull(correctImproveDTO.getCertAttachmentId())) {
            return ResponseMessage.errorMsg("整改说明和文件上传都要必填~");
        }
        CorrectImprove correctImprove = new CorrectImprove();
        BeanUtils.copyProperties(correctImproveDTO, correctImprove);
        correctImprove.setCertId(cert.getId());
        int insert = correctImproveMapper.insert(correctImprove);
        if (insert < 1) {
            return ResponseMessage.errorMsg("系统繁忙~请稍后再试~");
        }
        cert.setCertStatus(CERTING)
                .setCertStage(CORRECT_IMPROVE);
        if (certificationService.put(cert) < 1) {
            return ResponseMessage.errorMsg("系统繁忙~请稍后再试~");
        }
        return ResponseMessage.successMsg("新增整改提高成功~等待审核~");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseMessage<String> putImprove(CorrectImproveDTO correctImproveDTO, Integer userId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                CORRECT_IMPROVE, userId, null, CERTING
        );
        if (Objects.isNull(cert)) {
            return ResponseMessage.errorMsg("该记录现阶段不能被修改~");
        }
        CorrectImprove correctImprove = new CorrectImprove();
        BeanUtils.copyProperties(correctImproveDTO, correctImprove);
        correctImprove.setCertId(cert.getId());
        int update = correctImproveMapper.updateById(correctImprove);
        if (update < 1) {
            return ResponseMessage.errorMsg("系统繁忙~请稍后再试~");
        }
        return ResponseMessage.successMsg("修改成功~等待审核~");
    }

    @Override
    public ResponseMessage<CorrectImproveAdminVO> getImproveDetailAdmin(Integer certId) {
        Certification cert = certificationService.getOneCertByCertStageAndPrincipalId(
                CORRECT_IMPROVE, null, certId, CERTING
        );
        if (Objects.isNull(cert)) {
            return ResponseMessage.errorMsg("没有查到该进校考察记录~");
        }
        LambdaQueryWrapper<CorrectImprove> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true, CorrectImprove::getCertId, certId);
        CorrectImprove correctImprove = correctImproveMapper.selectOne(wrapper);
        if (Objects.isNull(correctImprove)) {
            return ResponseMessage.errorMsg("没有查到该记录~");
        }
        ResponseMessage<ProfessionSelfEvaluation> proDetailResponseMessage =
                professionSelfEvaluationService.getProDetailByCertId(certId);
        if (proDetailResponseMessage.isNotSuccess()) {
            return ResponseMessage.errorMsg(proDetailResponseMessage.getMsg());
        }
        ProfessionSelfEvaluation pro = proDetailResponseMessage.getData();
        CorrectImproveAdminVO correctImproveAdminVO = CorrectImproveAdminVO.builder()
                .firstLevelPoint(pro.getFirstLevelPoint())
                .secondLevelPoint(pro.getSecondLevelPoint())
                .correctImprove(correctImprove)
                .build();
        return ResponseMessage.success(correctImproveAdminVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseMessage<String> putImproveAdmin(Integer certId, Integer status, String remark) {
        ResponseMessage<CorrectImproveAdminVO> correctImproveAdminVOR = getImproveDetailAdmin(certId);
        if (correctImproveAdminVOR.isNotSuccess()) {
            return ResponseMessage.errorMsg(correctImproveAdminVOR.getMsg());
        }
        ResponseMessage<String> res = certificationService.putCertAdmin(certId, status, remark, CORRECT_IMPROVE);
        if (res.isNotSuccess()) {
            return ResponseMessage.errorMsg(res.getMsg());
        }
        return ResponseMessage.successMsg("操作成功~");
    }


}
